package com.mentaleye.controller;

import com.alibaba.fastjson.JSONObject;
import com.mentaleye.bean.InterflowBean;
import com.mentaleye.biz.DictionaryBiz;
import com.mentaleye.biz.InterflowBiz;
import com.mentaleye.biz.TeacherBiz;
import com.mentaleye.enums.DictionaryEnums;
import com.mentaleye.util.CommonPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;
import java.util.Map;

/**
 * Created by Rock on 2017/5/12.
 */
@Controller
@RequestMapping(value = "/interflow")
public class InterflowController {

    @Autowired private InterflowBiz interflowBiz;
    @Autowired private DictionaryBiz dictionaryBiz;
    @Autowired private TeacherBiz teacherBiz;

    @RequestMapping("/index")
    public ModelAndView index(){
        ModelAndView view = new ModelAndView("em/interflow/interflowList");
        Map<Integer, String> interflowTypeMap = dictionaryBiz.queryDictionaryByParent(
                DictionaryEnums.INTERFLOW_TYPE.getCode());
        Map<Integer, String> interflowWillMap = dictionaryBiz.queryDictionaryByParent(
                DictionaryEnums.INTERFLOW_WILL.getCode());
        view.addObject("interflowTypeMap",interflowTypeMap);
        view.addObject("interflowWillMap",interflowWillMap);
        view.addObject("interflowWill",JSONObject.toJSONString(interflowWillMap));
        view.addObject("interflowType",JSONObject.toJSONString(interflowTypeMap));
        return view;
    }

    @RequestMapping("/edit")
    public ModelAndView edit(@RequestParam(value = "interflowId",required = false) Integer interflowId){
        ModelAndView view = new ModelAndView("em/interflow/interflowEdit");
        Map<Integer, String> interflowTypeMap = dictionaryBiz.queryDictionaryByParent(
                DictionaryEnums.INTERFLOW_TYPE.getCode());
        Map<Integer, String> interflowWillMap = dictionaryBiz.queryDictionaryByParent(
                DictionaryEnums.INTERFLOW_WILL.getCode());
        List<Map<String, Object>> stuList = interflowBiz.queryNotStudent();
        view.addObject("teacherList",teacherBiz.queryAllTeacher());
        view.addObject("stuList",interflowBiz.queryNotStudent());
        view.addObject("typeMap",interflowTypeMap);
        view.addObject("willMap",interflowWillMap);
        if (interflowId != null){//修改
             view.addObject("bean",interflowBiz.query(interflowId));
             view.addObject("edit",true);
        }
        return view;
    }

    @RequestMapping("/save")
    @ResponseBody
    public String save(InterflowBean bean){
        return JSONObject.toJSONString(interflowBiz.edit(bean));
    }

    @RequestMapping("/remove")
    @ResponseBody
    public String remove(@RequestBody List<Integer> ids){
        return JSONObject.toJSONString(interflowBiz.removeList(ids));
    }

    @RequestMapping("/interflowListData")
    @ResponseBody
    public String interflowListData(CommonPage commonPage){
        interflowBiz.queryCommPage(commonPage);
        return commonPage.toSHowPage();
    }
}
